package com.lin.controller;

import com.lin.annotation.MyLog;
import com.lin.pojo.Category;
import com.lin.pojo.Wares;
import com.lin.services.CategoryService;
import com.lin.services.StockReportService;
import com.lin.utils.PageUnit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * ClassName:StockController
 * Package:com.lin.controller
 * Author: hellcat
 * Date: 2019/12/19
 * ♣♣♣  库存管理
 */
@Controller
@RequestMapping("/stockReport")
public class StockReportController {
    @Autowired
    private StockReportService stockReportService;
    @Autowired
    private CategoryService categoryService;

    @MyLog("查看库存报表")
    @RequiresPermissions("report:stockReport:list")
    @RequestMapping(value = "/toStockReport", method = RequestMethod.GET)
    public String toStock(HttpSession session, HttpServletRequest request) {
        request.setAttribute("ddId", "StockReport");
        List<Category> list1 = categoryService.getCategory1();
        request.setAttribute("Category1", list1);
        return "stockReportMain";
    }

    @RequiresPermissions("report:stockReport:list")
    @RequestMapping(value = "/getStockShowData", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getStockShowData(HttpServletRequest request) {
        return stockReportService.showStockData();
    }

    @RequiresPermissions("report:stockReport:list")
    @RequestMapping(value = "/getStock", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getStock(HttpServletRequest request) {
        String idOrName = request.getParameter("idOrName");
        String category1 = request.getParameter("category1");
        String category2 = request.getParameter("category2");
        int page = Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        List<Wares> list = stockReportService.getStock(idOrName, category1, category2);
        return PageUnit.Page(list, page, limit);
    }

    @RequiresPermissions("report:stockReport:list")
    @RequestMapping(value = "/loadStockReport", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> loadStockReport(HttpServletRequest request) {
        return stockReportService.loadStockReport();
    }

    @RequiresPermissions("report:stockReport:list")
    @RequestMapping(value = "/getCategory2", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getCategory2(HttpServletRequest request) {
        String firstCategoryId = request.getParameter("firstCategoryId");
        return categoryService.getCategory2(Integer.parseInt(firstCategoryId));
    }

    @MyLog("修改最小库存")
    @RequiresPermissions("report:stockReport:update")
    @RequestMapping(value = "/updateMinInventoryValue", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateMinInventory(HttpServletRequest request) {
        String id = request.getParameter("id");
        String minInventoryValue = request.getParameter("minInventoryValue");
        return stockReportService.updateMinInventory(id, minInventoryValue);
    }

    @MyLog("修改当前库存")
    @RequiresPermissions("report:stockReport:update")
    @RequestMapping(value = "/updateInventoryValue", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateInventory(HttpServletRequest request) {
        String id = request.getParameter("id");
        String inventoryValue = request.getParameter("inventoryValue");
        return stockReportService.updateInventory(id, inventoryValue);
    }
}
